package com.szgz.platform.service;

import com.szgz.platform.model.UserFile;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * 用户文件服务接口
 */
public interface UserFileService {
    
    /**
     * 获取用户的所有文件列表
     */
    List<UserFile> getUserFiles(String userId);
    
    /**
     * 获取用户指定目录下的文件
     */
    List<UserFile> getUserFilesInDirectory(String userId, String directory);
    
    /**
     * 获取用户的项目列表
     */
    List<UserFile> getUserProjects(String userId);
    
    /**
     * 获取文件内容
     */
    String getFileContent(String userId, String filePath);
    
    /**
     * 搜索用户文件
     */
    List<UserFile> searchUserFiles(String userId, String query, String searchType);
    
    /**
     * 获取文件统计信息
     */
    UserFileStats getUserFileStats(String userId);

    /**
     * 上传单个文件到 IDE 容器指定目录
     * @return 容器内目标路径
     */
    String uploadFileToIde(String userId, MultipartFile file, String targetDirectory);

    /**
     * 批量上传文件到 IDE 容器指定目录
     * @return 容器内目标路径列表
     */
    List<String> uploadFilesToIde(String userId, List<MultipartFile> files, String targetDirectory);

    /**
     * 批量上传文件到IDE容器
     */
    List<String> uploadMultipleFilesToIde(String userId, List<MultipartFile> files, String targetDirectory) throws Exception;

    /**
     * 获取用户的文件树结构
     */
    List<FileTreeNode> getUserFileTree(String userId, String rootDirectory);

    /**
     * 保存文件内容到 IDE 容器
     */
    boolean saveFileContent(String userId, String filePath, String content);

    /**
     * 在 IDE 容器中创建空文件
     */
    boolean createFile(String userId, String filePath);

    /**
     * 在 IDE 容器中创建目录（递归）
     */
    boolean createDirectory(String userId, String dirPath);

    /**
     * 在 IDE 容器中删除文件或目录
     * @param recursive 目录删除是否递归
     */
    boolean deletePath(String userId, String path, boolean recursive);

    /**
     * 文件树节点类
     */
    class FileTreeNode {
        private String name;
        private String path;
        private boolean isDirectory;
        private long size;
        private String fileType;
        private LocalDateTime lastModified;
        private List<FileTreeNode> children;

        public FileTreeNode() {
            this.children = new ArrayList<>();
        }

        public FileTreeNode(String name, String path, boolean isDirectory) {
            this.name = name;
            this.path = path;
            this.isDirectory = isDirectory;
            this.children = new ArrayList<>();
        }

        // Getters and Setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }

        public String getPath() { return path; }
        public void setPath(String path) { this.path = path; }

        public boolean isDirectory() { return isDirectory; }
        public void setDirectory(boolean directory) { isDirectory = directory; }

        public long getSize() { return size; }
        public void setSize(long size) { this.size = size; }

        public String getFileType() { return fileType; }
        public void setFileType(String fileType) { this.fileType = fileType; }

        public LocalDateTime getLastModified() { return lastModified; }
        public void setLastModified(LocalDateTime lastModified) { this.lastModified = lastModified; }

        public List<FileTreeNode> getChildren() { return children; }
        public void setChildren(List<FileTreeNode> children) { this.children = children; }

        public void addChild(FileTreeNode child) {
            this.children.add(child);
        }

        public String getReadableSize() {
            if (size < 1024) {
                return size + " B";
            } else if (size < 1024 * 1024) {
                return String.format("%.1f KB", size / 1024.0);
            } else if (size < 1024 * 1024 * 1024) {
                return String.format("%.1f MB", size / (1024.0 * 1024.0));
            } else {
                return String.format("%.1f GB", size / (1024.0 * 1024.0 * 1024.0));
            }
        }
    }

    /**
     * 文件统计信息类
     */
    class UserFileStats {
        private int totalFiles;
        private int totalDirectories;
        private long totalSize;
        private int codeFiles;
        private int documentFiles;
        private int imageFiles;
        private int otherFiles;
        
        // Getters and Setters
        public int getTotalFiles() { return totalFiles; }
        public void setTotalFiles(int totalFiles) { this.totalFiles = totalFiles; }
        
        public int getTotalDirectories() { return totalDirectories; }
        public void setTotalDirectories(int totalDirectories) { this.totalDirectories = totalDirectories; }
        
        public long getTotalSize() { return totalSize; }
        public void setTotalSize(long totalSize) { this.totalSize = totalSize; }
        
        public int getCodeFiles() { return codeFiles; }
        public void setCodeFiles(int codeFiles) { this.codeFiles = codeFiles; }
        
        public int getDocumentFiles() { return documentFiles; }
        public void setDocumentFiles(int documentFiles) { this.documentFiles = documentFiles; }
        
        public int getImageFiles() { return imageFiles; }
        public void setImageFiles(int imageFiles) { this.imageFiles = imageFiles; }
        
        public int getOtherFiles() { return otherFiles; }
        public void setOtherFiles(int otherFiles) { this.otherFiles = otherFiles; }
    }
} 